package com.xmut.bs.handaccount.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xmut.bs.handaccount.GlideApp;
import com.xmut.bs.handaccount.R;
import com.xmut.bs.handaccount.activity.AdvManageActivity;
import com.xmut.bs.handaccount.activity.CollectionManageActivity;
import com.xmut.bs.handaccount.activity.GoodsManageActivity;
import com.xmut.bs.handaccount.activity.LoginActivity;
import com.xmut.bs.handaccount.activity.MyArticleActivity;
import com.xmut.bs.handaccount.activity.MyCollectActivity;
import com.xmut.bs.handaccount.activity.MyFollowActivity;
import com.xmut.bs.handaccount.activity.OrderActivity;
import com.xmut.bs.handaccount.activity.PersonInfoActivity;
import com.xmut.bs.handaccount.activity.VerifyVideoListActivity;
import com.xmut.bs.handaccount.base.ToolbarBaseFragment;
import com.xmut.bs.handaccount.bmob.User;
import com.xmut.bs.handaccount.util.DisplayUtils;
import com.xmut.bs.handaccount.util.GlideCircleTransform;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

import static com.xmut.bs.handaccount.activity.OrderActivity.ORDER_TYPE;
import static com.xmut.bs.handaccount.activity.OrderActivity.ORDER_TYPE_PAY;
import static com.xmut.bs.handaccount.activity.OrderActivity.ORDER_TYPE_SERVICE;
import static com.xmut.bs.handaccount.activity.OrderActivity.ORDER_TYPE_USE;
import static com.xmut.bs.handaccount.base.Constants.PARAM_ORDER_MANAGER;

/**
 * Description
 * Created by Dollkey on 2018/4/11.
 */

public class PersonFragment extends ToolbarBaseFragment {

    @BindView(R.id.avatar_img)
    ImageView mAvatarImg;
    @BindView(R.id.username)
    TextView mUsername;
    @BindView(R.id.my_article)
    TextView mMyArticle;
    @BindView(R.id.my_collect)
    TextView mMyCollect;
    @BindView(R.id.my_follow)
    TextView mMyFollow;
    @BindView(R.id.mask_view)
    View mMaskView;
    @BindView(R.id.type_name_tv)
    TextView mTypeNameTv;
    @BindView(R.id.up_img_tv1)
    TextView mUpImgTv1;
    @BindView(R.id.up_img_tv2)
    TextView mUpImgTv2;
    @BindView(R.id.up_img_tv3)
    TextView mUpImgTv3;
    @BindView(R.id.up_img_tv4)
    TextView mUpImgTv4;
    @BindView(R.id.add_collection)
    TextView mAddCollection;
    @BindView(R.id.add_adv)
    TextView mAddAdv;
    @BindView(R.id.order_verify)
    TextView mOrderVerify;
    @BindView(R.id.video_verify)
    TextView mVideoVerify;
    @BindView(R.id.goods_manage)
    TextView mGoodsManage;
    @BindView(R.id.manager_ll)
    LinearLayout mManagerLl;

    private User user;
    private int width;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_person;
    }

    @Override
    protected void init() {
        initIconFont(mMyArticle, mMyCollect, mMyFollow, mUpImgTv1, mUpImgTv2, mUpImgTv3, mUpImgTv4,
                mTypeNameTv, mAddCollection, mOrderVerify, mVideoVerify, mAddAdv, mGoodsManage);
        width = DisplayUtils.dp2px(mContext, 60);
    }

    @Override
    public void onResume() {
        super.onResume();
        user = BmobUser.getCurrentUser(User.class);
        if(user==null){
            initUser();
            return;
        }
        user.setPassword("111111");
        user.login(new SaveListener<User>() {
            @Override
            public void done(User o, BmobException e) {
                if (e == null) {
                    user = o;
                }
                initUser();
            }
        });
    }

    private void initUser() {
        GlideApp.with(mAvatarImg).load(user == null ? "" : user.getAvatar()).placeholder(R.mipmap.ic_head_img).error(R.mipmap.ic_head_img)
                .override(width, width).transform(new GlideCircleTransform(mContext)).into(mAvatarImg);
        mMyArticle.setText(getResources().getString(R.string.ic_article).concat("   我的文章"));
        mMyCollect.setText(getResources().getString(R.string.ic_collect).concat("   我的收藏"));
        mMyFollow.setText(getResources().getString(R.string.ic_follow).concat("   我的关注"));
        mTypeNameTv.setText(getResources().getString(R.string.ic_order).concat("   我的订单"));
        mGoodsManage.setText(getResources().getString(R.string.ic_goods_manage).concat("   商品管理"));
        mAddCollection.setText(getResources().getString(R.string.ic_add_collection).concat("   合辑管理"));
        mAddAdv.setText(getResources().getString(R.string.ic_add_adv).concat("   广告管理"));
        mOrderVerify.setText(getResources().getString(R.string.ic_o_verify).concat("   订单管理"));
        mVideoVerify.setText(getResources().getString(R.string.ic_video_verify).concat("   视频审核"));
        if (user != null) {
            mMaskView.setVisibility(View.GONE);
            mUsername.setText(user.getNickName());
            mManagerLl.setVisibility(user.getManager() ? View.VISIBLE : View.GONE);
        } else {
            mUsername.setText("游客");
            mManagerLl.setVisibility(View.GONE);
            mMaskView.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.avatar_img, R.id.username, R.id.my_article, R.id.my_collect, R.id.my_follow,
            R.id.mask_view, R.id.type_name_tv, R.id.pll1, R.id.pll2, R.id.pll3, R.id.pll4,
            R.id.add_collection, R.id.order_verify, R.id.video_verify, R.id.add_adv, R.id.goods_manage})
    public void onViewClicked(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.avatar_img:
            case R.id.username:
                startActivity(PersonInfoActivity.class);
                break;
            case R.id.my_article:
                startActivity(MyArticleActivity.class);
                break;
            case R.id.my_collect:
                startActivity(MyCollectActivity.class);
                break;
            case R.id.my_follow:
                startActivity(MyFollowActivity.class);
                break;
            case R.id.mask_view:
                startActivity(LoginActivity.class);
                break;
            case R.id.type_name_tv:
                startActivity(OrderActivity.class);
                break;
            case R.id.pll1:
                startActivity(OrderActivity.class);
                break;
            case R.id.pll2:
                startActivity(intent.putExtra(ORDER_TYPE, ORDER_TYPE_PAY), OrderActivity.class);
                break;
            case R.id.pll3:
                startActivity(intent.putExtra(ORDER_TYPE, ORDER_TYPE_USE), OrderActivity.class);
                break;
            case R.id.pll4:
                startActivity(intent.putExtra(ORDER_TYPE, ORDER_TYPE_SERVICE), OrderActivity.class);
                break;
            case R.id.add_collection:
                startActivity(CollectionManageActivity.class);
                break;
            case R.id.add_adv:
                startActivity(AdvManageActivity.class);
                break;
            case R.id.order_verify:
                startActivity(intent.putExtra(PARAM_ORDER_MANAGER, true), OrderActivity.class);
                break;
            case R.id.video_verify:
                startActivity(VerifyVideoListActivity.class);
                break;
            case R.id.goods_manage:
                startActivity(GoodsManageActivity.class);
                break;
        }
    }

    @Override
    protected String getSubTitle() {
        return "";
    }

    @Override
    protected boolean isShapeIcon() {
        return true;
    }

    public void initIconFont(TextView... views) {
        for (TextView view : views) {
            view.setTypeface(iconfont);
        }
    }
}
